package com.zjdiepu.www.biz.dh;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhInsteadPaymentMapper;
import com.zjdiepu.www.ibiz.dh.IDhInsteadPaymentService;
import com.zjdiepu.www.model.dh.dto.DhInsteadPayment;
import com.zjdiepu.www.model.dh.ext.DhInsteadPaymentExt;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年05月22日 16时22分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhInsteadPaymentServiceImpl extends BaseService implements IDhInsteadPaymentService {
	public static final Logger logger = LoggerFactory.getLogger(DhInsteadPaymentServiceImpl.class);

	@Autowired
	private DhInsteadPaymentMapper dhInsteadPaymentMapper;
	
	@Override
	public void saveUpdateAndDelete(DhInsteadPayment dhInsteadPayment, OperateType operType) {
		switch (operType) {
		case add:
			dhInsteadPayment.initInsertData(dhInsteadPayment.getModifier(), dhInsteadPayment.getModifierName());
			dhInsteadPaymentMapper.insert(dhInsteadPayment);
			break;
		case edit:
			dhInsteadPayment.initUpdateData(dhInsteadPayment.getModifier(), dhInsteadPayment.getModifierName());
			// 影响的记录数
			int updateCount = dhInsteadPaymentMapper.updateByPrimaryKeySelective(dhInsteadPayment);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhInsteadPayment.initDeleteData(dhInsteadPayment.getModifier(), dhInsteadPayment.getModifierName());
			// 影响的记录数
			int deleteCount = dhInsteadPaymentMapper.updateByPrimaryKeySelective(dhInsteadPayment);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhInsteadPayment> selectByExt(DhInsteadPaymentExt dhInsteadPaymentExt) {
		return dhInsteadPaymentMapper.selectByExt(dhInsteadPaymentExt);
	}
	
	@Override
	public int countByExt(DhInsteadPaymentExt dhInsteadPaymentExt){
		return dhInsteadPaymentMapper.countByExt(dhInsteadPaymentExt);
	}
	
	@Override
	public DhInsteadPayment selectById(String id) {
		return dhInsteadPaymentMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhInsteadPaymentMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhInsteadPayment dhInsteadPayment) {
		return dhInsteadPaymentMapper.countRefined(dhInsteadPayment);
	}

	@Override
	public DhInsteadPayment selectRefined(DhInsteadPayment dhInsteadPayment) {
		return dhInsteadPaymentMapper.selectRefined(dhInsteadPayment);
	}

	@Override
	public boolean repeat(DhInsteadPayment dhInsteadPayment) {
		int count = countRefined(dhInsteadPayment);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhInsteadPayment.getId())) {
			return true;
		}
		DhInsteadPayment dbItem = selectRefined(dhInsteadPayment);
		if(null == dbItem) {
			return false;
		}
		return !dhInsteadPayment.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhInsteadPaymentMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}

	@Override
	public void insertBatch(List<DhInsteadPayment> listPayment) {
		dhInsteadPaymentMapper.insertBatch(listPayment);
		
	}

	
}
